//
//  FollowTipsView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/6.
//

#import "FollowTipsView.h"
#import "YPLabel.h"

@interface FollowTipsView ()

/// 1全自动；2半自动
@property (nonatomic, assign) NSInteger type;

@property (nonatomic, strong) UIView *bgview;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView *topImageV;
@property (nonatomic, strong) UIButton *followBtn;
@property (nonatomic, strong) UIView *updateView;
@property (nonatomic, strong) UIButton *fullAutoBtn;
@property (nonatomic, strong) UIButton *halfAutoBtn;
@property (nonatomic, strong) YPLabel *contentLabel;

@end

@implementation FollowTipsView

- (void)initData {
    self.backgroundColor = [UIColor clearColor];
    self.type = 1;
}

- (void)initViews {
    [self addSubview:self.bgview];
    [self.bgview addSubview:self.contentView];
    [self.bgview addSubview:self.topImageV];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 68, kWidthForScreen, 20)];
    titleLabel.font = kFontSemiBold(20);
    titleLabel.textColor = kRGB(0x333333);
    titleLabel.text = @"重要提示";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgview addSubview:titleLabel];
    [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:closeBtn];
    
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.updateView];
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"更新方式";
    tipLabel.font = kFontMediium(14);
    tipLabel.textColor = kRGB(0x333333);
    [self.updateView addSubview:tipLabel];
    
    [self.updateView addSubview:self.fullAutoBtn];
    [self.updateView addSubview:self.halfAutoBtn];
    [self.contentView addSubview:self.followBtn];
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((kWidthForScreen-72), 50));
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-kHeightForTabbarExtra);
    }];
    [self.updateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.trailing.mas_equalTo(-16);
        make.bottom.mas_equalTo(self.followBtn.mas_top).offset(-16);
        make.height.mas_equalTo(60);
    }];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.leading.mas_equalTo(10);
    }];
    [self.halfAutoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(110, 20));
    }];
    [self.fullAutoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.halfAutoBtn.mas_leading).offset(-10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(110, 20));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.trailing.mas_equalTo(-16);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(30);
        make.bottom.mas_equalTo(self.updateView.mas_top).offset(-15);
    }];
}

- (UIView *)bgview {
    if (!_bgview) {
        _bgview = [[UIView alloc] initWithFrame:CGRectMake(0, kHeightForScreen-540, kWidthForScreen, 540)];
        _bgview.backgroundColor = [UIColor whiteColor];
    }
    return _bgview;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, kWidthForScreen, 490)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [_contentView addTopCorner:10];
    }
    return _contentView;
}

- (UIImageView *)topImageV {
    if (!_topImageV) {
        _topImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"followtips"]];
        _topImageV.frame = CGRectMake(0, 0, kWidthForScreen, 114);
        _topImageV.contentMode = UIViewContentModeScaleAspectFill;
        _topImageV.clipsToBounds = YES;
    }
    return _topImageV;
}

- (UIButton *)followBtn {
    if (!_followBtn) {
        _followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _followBtn.backgroundColor = kRGB(0xFF6081);
        _followBtn.layer.cornerRadius = 25;
        [_followBtn setTitle:@"+关注" forState:UIControlStateNormal];
        [_followBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _followBtn.titleLabel.font = kFontMediium(16);
        @weakify(self)
        [[_followBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.followBlock) {
                self.followBlock(self.type);
            }
        }];
    }
    return _followBtn;
}

- (UIView *)updateView {
    if (!_updateView) {
        _updateView = [[UIView alloc] init];
        _updateView.backgroundColor = kRGB(0xF7F7F7);
        _updateView.layer.cornerRadius = 8;
        _updateView.layer.masksToBounds = YES;
    }
    return _updateView;
}

- (UIButton *)fullAutoBtn {
    if (!_fullAutoBtn) {
        _fullAutoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fullAutoBtn.tag = 501;
        [_fullAutoBtn setImage:[UIImage imageNamed:@"followTipBtn"] forState:UIControlStateNormal];
        [_fullAutoBtn setImage:[UIImage imageNamed:@"followTipBtn_s"] forState:UIControlStateSelected];
        [_fullAutoBtn setTitle:@"全自动更新" forState:UIControlStateNormal];
        [_fullAutoBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _fullAutoBtn.titleLabel.font = kFontRegula(14);
        _fullAutoBtn.selected = YES;
        [_fullAutoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 5)];
        _fullAutoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        @weakify(self)
        [[_fullAutoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self autoBtnAction:x];
        }];
    }
    return _fullAutoBtn;
}

- (UIButton *)halfAutoBtn {
    if (!_halfAutoBtn) {
        _halfAutoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _halfAutoBtn.tag = 502;
        [_halfAutoBtn setImage:[UIImage imageNamed:@"followTipBtn"] forState:UIControlStateNormal];
        [_halfAutoBtn setImage:[UIImage imageNamed:@"followTipBtn_s"] forState:UIControlStateSelected];
        [_halfAutoBtn setTitle:@"半自动更新" forState:UIControlStateNormal];
        [_halfAutoBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _halfAutoBtn.titleLabel.font = kFontRegula(14);
        [_halfAutoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 5)];
        _halfAutoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        @weakify(self)
        [[_halfAutoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self autoBtnAction:x];
        }];
    }
    return _halfAutoBtn;
}

- (YPLabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[YPLabel alloc] init];
        _contentLabel.textColor = kRGB(0x666666);
        _contentLabel.font = kFontRegula(12);
        _contentLabel.numberOfLines = 0;
        [_contentLabel setVerAlignment:VerticalAlgnment_top];
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.lineSpacing = 10;
        
        NSString *str = @"请选择信息同步方式，具体区别如下：\n1. 全自动(默认选择)：只要关注了商家，商家上新、下架、删除、信息修改(文字/视频/图片)都会同步给关注者；也就是说之后只要关注了该商家，不用再进行任何操作，ma里边的商品信息会自动更新。\n2. 半自动更新：关注了商家，商家上新需要手动审核内容并转图，之后该商品的信息(图片/视频/文字)的更新将以提醒的方式供中间商手动决定是否更新；转过的商品的下架，删除还是自动同步的。\n3. 下级可以在之后的实际使用过程中更改信息同步方式。\n4. 任何情况下取消关注都会失去上级商家的商品。";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange range = [str rangeOfString:@"3. 下级可以在之后的实际使用过程中更改信息同步方式。\n4. 任何情况下取消关注都会失去上级商家的商品。"];
        [attr addAttributes:@{NSParagraphStyleAttributeName:paragraph} range:NSMakeRange(0, str.length)];
        [attr addAttributes:@{NSForegroundColorAttributeName:kRGB(0xFF9754),NSFontAttributeName:kFontMediium(12)} range:range];
        
        _contentLabel.attributedText = attr;
        
    }
    return _contentLabel;
}

- (void)show {
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    self.tag = 9999;
    self.hidden = NO;
    self.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
    }];
}
- (void)hide {
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:self];
    if (!CGRectContainsPoint(self.bgview.frame, point)) {
        [self hide];
    }
}

- (void)autoBtnAction:(UIButton *)sender {
    self.halfAutoBtn.selected = NO;
    self.fullAutoBtn.selected = NO;
    sender.selected = YES;
    self.type = sender.tag-500;
}

@end
